<?php

namespace backend\models\service;

use Yii;
use yii\data\Sort;
use backend\models\odm\TitleOdm as Title;

class TitleService extends Title {

    private $_query;
   

    public function init() {
        parent::init();
        $this->_query = static::find();
    }


    public function search() {
        $query = Admin::find();
        $query->andFilterWhere([
            'id' => $this->id,
        ]);

        return $query;
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function getQuerySearch($params) {
        $this->load($params);
        $this->_query->andFilterWhere([
            'id' => $this->id,
        ]);

        return $this;
    }

    /**
     * 排序/分页
     * 
     * @return []
     */
    public function getQueryDataprovider() {
        $sort = new Sort([
            'defaultOrder' => ['create_time' => SORT_DESC],
            'attributes' => [
                'create_time',
            ],
        ]);
        $this->_query->orderBy($sort->orders);

        $dataProvider = new ActiveDataProvider([
            'query' => $this->_query,
            'sort' => $sort,
            'pagination' => [
                'pagesize' => 10,
            ]
        ]);

        $data = [];
        if (!empty($dataProvider->models))
            foreach ($dataProvider->models as $key => $model) {
                $date = date("Y.m.d", $model->create_time);
                $data[$date][] = $model;
            }
        return ['dataProvider' => $dataProvider, 'data' => $data];
    }
    
    /**
     * 获取所有标题
     */
    public static function getAll()
    {
        $model = self::find();
        $model->andWhere(['status'=>1]);
        $data = $model->asArray()->all();
        return $data;
    }
    
}
